package salixtest.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.*;
import com.googlecode.salix.client.Tree;
import com.googlecode.salix.client.TreeConfig;
import com.googlecode.salix.client.TreeItem;

import java.util.Comparator;

/**
 * DefaultTree.
 *
 * @author Richard Richter
 */
public class DefaultTree extends Composite {
	public DefaultTree(boolean sort) {
		HorizontalPanel panel = new HorizontalPanel();
		panel.setStyleName("example-main");

		final SimplePanel targetPanel = new SimplePanel();

		final Tree tree = SalixTest.createTree(new TreeConfig()
			.setClassTreeItem("salix-item")
			.setClassTreeItemSelected("salix-item-selected")
			.setShowConnectors(true));
		if (sort) {
			tree.configuration().setComparator(new Comparator<TreeItem>() {
				public int compare(TreeItem n1, TreeItem n2) {
					if (n1.getWidget() == null) {
						return -1;
					} else if (n2.getWidget() == null) {
						return 1;
					} else if (n1.getWidget() instanceof Label && n2.getWidget() instanceof Label) {
						return (((Label) n1.getWidget()).getText().compareToIgnoreCase(((Label) n2.getWidget()).getText()));
					} else {
						return 0; //ak to nei su labely tak nech su rovnake pri zaradovani - v tomot pripade to nenastava
					}
				}
			});
		}

		tree.addSelectionHandler(new SelectionHandler<TreeItem>() {
			public void onSelection(SelectionEvent<TreeItem> event) {
				targetPanel.setWidget(new Label(event.getSelectedItem().getWidget().toString()));
			}
		});
		tree.draw();

		tree.setStyleName("example-tree");
		targetPanel.setStyleName("example-target");

		Button addChildButton = new Button("Add child");
		addChildButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				addChild(tree);
			}
		});
		Button removeButton = new Button("Remove selected");
		removeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				tree.removeSelected();
			}
		});
		Button removeChildrenButton = new Button("Remove all children");
		removeChildrenButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				tree.getSelectedItem().removeItems();
			}
		});
		Button unselectButton = new Button("Unselect");
		unselectButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				tree.setSelectedItem(null);
			}
		});

		FlowPanel fp = new FlowPanel();
		fp.add(tree);
		fp.add(addChildButton);
		fp.add(removeButton);
		fp.add(removeChildrenButton);
		fp.add(unselectButton);
		panel.add(fp);
		panel.add(targetPanel);

		initWidget(panel);
	}

	private static void addChild(final Tree tree) {
		final DialogBox addModalPanel = new DialogBox(false, true);
		addModalPanel.setText("Add child");
		final TextBox input = new TextBox();
		Button okBtn = new Button("OK");
		okBtn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				TreeItem addedTreeItem;
				if (tree.getSelectedItem() != null) {
					addedTreeItem = tree.getSelectedItem().addItem(input.getText());
					tree.getSelectedItem().expand();
				} else {
					addedTreeItem = tree.addItem(input.getText());
				}
				addedTreeItem.select();
				addModalPanel.hide();
			}
		});

		Button cancelBtn = new Button("Cancel");
		cancelBtn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				addModalPanel.hide();
			}
		});
		HorizontalPanel hp = new HorizontalPanel();
		hp.add(okBtn);
		hp.add(cancelBtn);

		FlowPanel fp = new FlowPanel();
		fp.add(input);
		fp.add(hp);

		addModalPanel.add(fp);
		addModalPanel.center();
		input.setFocus(true);
	}
}
